package com.netafull;

import java.util.List;

import com.netafull.analyze.Ads;
import com.netafull.analyze.AnalyzeXMLForAd;
import com.netafull.util.CloseUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;

public class Welcome extends Activity {
	Thread t;
	List<Ads> ads;
	AnalyzeXMLForAd analyze;
	String path;
	int adId;

	Intent intent;
	
	boolean b = false;
	SharedPreferences sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		sp = getSharedPreferences("netafull",Context.MODE_PRIVATE);
		CloseUtil.activityList.add(Welcome.this);
		
		path = getResources().getString(R.string.getadlsit_url) + "?device=Android";
		intent = new Intent(Welcome.this, HomePage.class);
		analyze = new AnalyzeXMLForAd();
		AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);
		aa.setDuration(3000);
		this.findViewById(R.id.image_welcome).startAnimation(aa);
		aa.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationEnd(Animation arg0) {
				Welcome.this.startActivity(intent);
				finish();
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {

			}

			@Override
			public void onAnimationStart(Animation arg0) {
				new Thread() {
					@Override
					public void run() {
						super.run();
						try {
							//System.out.println("开始请求广告" + path);
							ads = analyze.getRss(path);
							if (ads.size() != 0) {
								adId = ads.get(0).getAd_id();
								System.out.println("开始解析广告---------->" + adId);
								if (adId != 0) {
									ShowAdActivity.ad_id = adId;
								} else {
									ShowAdActivity.ad_id = 1;
								}
							} else {
								ShowAdActivity.ad_id = 1;
							}
						} catch (Exception e) {
							e.printStackTrace();
							ShowAdActivity.ad_id = 1;
						}
					}
				}.start();
			}

		});
	}
	@Override
	protected void onUserLeaveHint() {
		// TODO Auto-generated method stub
		super.onUserLeaveHint();
		if(!b){
			if(sp!=null){
				Editor editor = sp.edit();
				editor.clear();
				editor.commit();
			}
			System.out.println("开始销毁Activity");
			CloseUtil.closeActivity();
		}else{
			System.out.println("没有销毁Activity");
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		b = false;
	}
}